<template>
	<yb-popup :options="optionsSync" :duration="optionsSync.duration" direction="bottom" :visible="visible" @hide="hide">
		<view class="yb-flex" :style="{
			'background-color': optionsSync.bgColor,
			'border-top-left-radius': Util.pixelunit(optionsSync.radius),
			'border-top-right-radius': Util.pixelunit(optionsSync.radius)
		}">
			<view class="yb-picker yb-flex yb-row yb-align-center yb-justify-between" :style="{
				'border-top-left-radius': Util.pixelunit(optionsSync.radius),
				'border-top-right-radius': Util.pixelunit(optionsSync.radius)
			}">
				<yb-tap :disabled="cancelSync.disabled" @click="pickerCancel">
					<text :style="{
						'font-size': Util.pixelunit(cancelSync.size),
						color: cancelSync.color,
						opacity: cancelSync.disabled ? 0.5 : 1
					}">{{cancelSync.text}}</text>
				</yb-tap>
				<text :style="{
					'font-size': Util.pixelunit(titleSync.size),
					color: titleSync.color
				}">{{titleSync.text}}</text>
				<yb-tap :disabled="confirmSync.disabled" @tap="pickerConfirm">
					<text :style="{
						'font-size': Util.pixelunit(confirmSync.size),
						color: confirmSync.color,
						opacity: confirmSync.disabled ? 0.5 : 1
					}">{{confirmSync.text}}</text>
				</yb-tap>
			</view>
			<yb-picker-view :options="optionsSync" :value="value" @change="onChange" :cascade="cascade" :data="itemList"></yb-picker-view>
			<yb-safe-bottom></yb-safe-bottom>
		</view>
	</yb-popup>
</template>

<script>
	import Util from '../../js_sdk/util.js'
	import ComponentMixin from '../../js_sdk/componentMixin.js'
	export default {
		mixins: [ComponentMixin],
		data () {
			return {
				visible: false,
				cascade: 0,
				value: [],
				values: [],
				title: '',
				itemList: [],
				options: {},
				success: null,
				fail: null
			}
		},
		computed: {
			Util () {
				return Util
			},
			titleSync () {
				return Object.assign({}, {
					text: '',
					color: '#666',
					size: '36rpx',
				}, Util.typeof(this.title) == 'Object' ? this.title : Util.typeof(this.title) == 'String' ? {text: this.title} : {})
			},
			confirmSync () {
				return Object.assign({}, {
					text: '确认',
					disabled: false,
					color: '#1989FA',
					size: '32rpx'
				}, Util.typeof(this.confirm) == 'Object' ? this.confirm : Util.typeof(this.confirm) == 'Boolean' ? {disabled: this.confirm} : Util.typeof(this.confirm) == 'String' ? {text: this.confirm} : {})
			},
			cancelSync () {
				return Object.assign({}, {
					text: '取消',
					disabled: false,
					color: '#333',
					size: '32rpx'
				}, Util.typeof(this.cancel) == 'Object' ? this.cancel : Util.typeof(this.cancel) == 'Boolean' ? {disabled: this.cancel} : Util.typeof(this.cancel) == 'String' ? {text: this.optionsSync.cancel} : {})
			},
			optionsSync () {
				return Object.assign({}, {
					popout: 'fade',
					duration: 300,
					bgColor: '#fff',
					radius: '20rpx'
				}, this.options)
			}
		},
		methods: {
			default ({title, value, itemList, cascade, confirm, cancel, success, fail, options}) {
				this.title = title || ''
				this.value = value || []
				this.itemList = itemList || []
				this.cascade = cascade || 0
				this.confirm = confirm || '确认'
				this.cancel = cancel || '取消'
				this.success = success || null
				this.fail = fail || null
				this.options = options || {}
				this.visible = true
				this.values = []
				if ( cascade > 0 ) {
					const dowhile = (data, key) => {
						let index = this.value.length > 0 ? this.value[key] : 0
						this.values.push({
							label: data[index].label,
							value: data[index].value
						})
						if ( key < cascade - 1 ) {
							dowhile(data[index].child, key + 1)
						}
					}
					dowhile(this.itemList, 0)
				} else {
					this.itemList.forEach((items, key) => {
						let index = this.value.length > 0 ? this.value[key] : 0
						this.values.push({
							index: index,
							label: items[index].label,
							value: items[index].value
						})
					})
				}
			},
			time ({title, value, start='00:00:00', end='23:59:59', formats='hh:mm:ss', confirm, cancel, success, fail, options}) {
				let dates = []
				let arr = formats.split(':')
				let startObj = start.split(':')
				let endObj = end.split(':')
				if ( startObj.length == 3 ) {
					startObj = {
						hours: startObj[0],
						minutes: startObj[1],
						seconds: startObj[2]
					}
				} else if ( startObj.length == 2 ) {
					if ( arr.indexOf('hh') > -1 ) {
						startObj = {
							hours: startObj[0],
							minutes: startObj[1]
						}
					} else if ( arr.indexOf('mm') > -1 ) {
						startObj = {
							minutes: startObj[0],
							seconds: startObj[1]
						}
					} else {
						startObj = {
							seconds: startObj[1]
						}
					}
				} else {
					if ( arr.indexOf('hh') > -1 ) {
						startObj = {
							hours: startObj[0]
						}
					} else if ( arr.indexOf('mm') > -1 ) {
						startObj = {
							minutes: startObj[0]
						}
					} else {
						startObj = {
							seconds: startObj[0]
						}
					}
				}
				if ( endObj.length == 3 ) {
					endObj = {
						hours: endObj[0],
						minutes: endObj[1],
						seconds: endObj[2]
					}
				} else if ( endObj.length == 2 ) {
					if ( arr.indexOf('hh') > -1 ) {
						endObj = {
							hours: endObj[0],
							minutes: endObj[1]
						}
					} else if ( arr.indexOf('mm') > -1 ) {
						endObj = {
							minutes: endObj[0],
							seconds: endObj[1]
						}
					} else {
						endObj = {
							seconds: endObj[1]
						}
					}
				} else {
					if ( arr.indexOf('hh') > -1 ) {
						endObj = {
							hours: endObj[0]
						}
					} else if ( arr.indexOf('mm') > -1 ) {
						endObj = {
							minutes: endObj[0]
						}
					} else {
						endObj = {
							seconds: endObj[0]
						}
					}
				}
				if ( formats.indexOf('hh') > -1 ) {
					let hours = parseInt(startObj.hours) - 1
					while ( hours != parseInt(endObj.hours) ) {
						if ( hours < 23 ) {
							hours++
						} else {
							hours = 0
						}
						dates.push({
							label: Util.zeroize(hours) + '时',
							value: Util.zeroize(hours),
							child: []
						})
						if ( formats.indexOf('mm') > -1 ) {
							let minutes = Util.zeroize(hours) == startObj.hours ? parseInt(startObj.minutes) - 1 : -1
							while ( minutes < (Util.zeroize(hours) == endObj.hours ? parseInt(endObj.minutes) : 59) ) {
								minutes++
								dates[dates.length-1].child.push({
									label: Util.zeroize(minutes) + '分',
									value: Util.zeroize(minutes),
									child: []
								})
								if ( formats.indexOf('ss') > -1 ) {
									let seconds = (Util.zeroize(hours) == startObj.hours && Util.zeroize(minutes) == startObj.minutes) ? parseInt(startObj.seconds) - 1 : -1
									while ( seconds < (( Util.zeroize(hours) == endObj.hours && Util.zeroize(minutes) == endObj.minutes) ? parseInt(endObj.seconds) : 59) ) {
										seconds++
										dates[dates.length-1].child[dates[dates.length-1].child.length-1].child.push({
											label: Util.zeroize(seconds) + '秒',
											value: Util.zeroize(seconds)
										})
									}
								}
							}
						}
					}
				} else if ( formats.indexOf('mm') > -1 ) {
					let minutes = parseInt(startObj.minutes) - 1
					while ( minutes < parseInt(endObj.minutes) ) {
						minutes++
						dates.push({
							label: Util.zeroize(minutes) + '分',
							value: Util.zeroize(minutes),
							child: []
						})
						if ( formats.indexOf('ss') > -1 ) {
							let seconds = Util.zeroize(minutes) == startObj.minutes ? parseInt(startObj.seconds) - 1 : -1
							while ( seconds < (Util.zeroize(minutes) == endObj.minutes ? parseInt(endObj.seconds) : 59) ) {
								seconds++
								dates[dates.length-1].child.push({
									label: Util.zeroize(seconds) + '秒',
									value: Util.zeroize(seconds)
								})
							}
						}
					}
				} else {
					let seconds = parseInt(startObj.seconds) - 1
					while ( seconds < parseInt(endObj.seconds) ) {
						seconds++
						dates.push({
							label: Util.zeroize(seconds) + '秒',
							value: Util.zeroize(seconds)
						})
					}
				}
				this.default({
					title: title || '选择时间',
					value: value,
					itemList: dates,
					cascade: arr.length,
					confirm: confirm,
					cancel: cancel,
					success: success,
					fail: fail,
					options: options
				})
			},
			date ({title, value, start='1970-01-01', end, formats='yyyy-mm-dd', confirm, cancel, success, fail, options}) {
				let dates = []
				let arr = formats.split('-')
				let startObj = start.split('-')
				let endObj = end ? end.split('-') : Util.dateFormat(new Date().getTime(), 'yyyy-mm-dd').split('-')
				if ( new Date(start).getTime() > (end ? new Date(end).getTime() : new Date().getTime()) ) {
					console.log('初始时间不能大于结束时间');
					return
				}
				if ( startObj.length == 3 ) {
					startObj = {
						years: startObj[0],
						month: startObj[1],
						date: startObj[2]
					}
				} else if ( startObj.length == 2 ) {
					if ( arr.indexOf('yyyy') > -1 ) {
						startObj = {
							years: startObj[0],
							month: startObj[1]
						}
					} else if ( arr.indexOf('mm') > -1 ) {
						startObj = {
							month: startObj[0],
							date: startObj[1]
						}
					} else {
						startObj = {
							date: startObj[1]
						}
					}
				} else {
					if ( arr.indexOf('yyyy') > -1 ) {
						startObj = {
							years: startObj[0]
						}
					} else if ( arr.indexOf('mm') > -1 ) {
						startObj = {
							month: startObj[0]
						}
					} else {
						startObj = {
							date: startObj[0]
						}
					}
				}
				if ( endObj.length == 3 ) {
					endObj = {
						years: endObj[0],
						month: endObj[1],
						date: endObj[2]
					}
				} else if ( endObj.length == 2 ) {
					if ( arr.indexOf('yyyy') > -1 ) {
						endObj = {
							years: endObj[0],
							month: endObj[1]
						}
					} else if ( arr.indexOf('mm') > -1 ) {
						endObj = {
							month: endObj[0],
							date: endObj[1]
						}
					} else {
						endObj = {
							date: endObj[1]
						}
					}
				} else {
					if ( arr.indexOf('yyyy') > -1 ) {
						endObj = {
							years: endObj[0]
						}
					} else if ( arr.indexOf('mm') > -1 ) {
						endObj = {
							month: endObj[0]
						}
					} else {
						endObj = {
							date: endObj[0]
						}
					}
				}
				if ( formats.indexOf('yyyy') > -1 ) {
					let years = parseInt(startObj.years) - 1
					while ( years < parseInt(endObj.years) ) {
						years++
						dates.push({
							label: Util.zeroize(years) + '年',
							value: Util.zeroize(years),
							child: []
						})
						if ( formats.indexOf('mm') > -1 ) {
							let month = Util.zeroize(years) == startObj.years ? parseInt(startObj.month) - 1 : 0
							while ( month < (Util.zeroize(years) == endObj.years ? parseInt(endObj.month) : 12 ) ) {
								month++
								dates[dates.length - 1].child.push({
									label: Util.zeroize(month) + '月',
									value: Util.zeroize(month),
									child: []
								})
								if ( formats.indexOf('dd') > -1 ) {
									let endDate = new Date(Util.zeroize(years),Util.zeroize(month),0).getDate()
									let date = (Util.zeroize(years) == startObj.years && Util.zeroize(month) == startObj.month) ? parseInt(startObj.date) - 1 : 0
									while ( date < ( (Util.zeroize(years) == endObj.years && Util.zeroize(month) == endObj.month) ? parseInt(endObj.date) : endDate ) ) {
										date++
										dates[dates.length - 1].child[dates[dates.length - 1].child.length - 1].child.push({
											label: Util.zeroize(date) + '日',
											value: Util.zeroize(date)
										})
									}
								}
							}
						}
					}
				} else if ( formats.indexOf('mm') > -1 ) {
					let month = parseInt(startObj.month) - 1
					while ( month < parseInt(endObj.month) ) {
						month++
						dates.push({
							label: Util.zeroize(month) + '月',
							value: Util.zeroize(month),
							child: []
						})
						if ( formats.indexOf('dd') > -1 ) {
							let endDate = new Date(new Date().getFullYear(),Util.zeroize(month),0).getDate()
							let date = (Util.zeroize(month) == startObj.month) ? parseInt(startObj.date) - 1 : 0
							while ( date < ( Util.zeroize(month) == endObj.month ? parseInt(endObj.date) : endDate ) ) {
								date++
								dates[dates.length - 1].child.push({
									label: Util.zeroize(date) + '日',
									value: Util.zeroize(date)
								})
							}
						}
					}
				} else {
					let endDate = new Date(new Date().getFullYear(),new Date().getMonth() + 1,0).getDate()
					let date = parseInt(startObj.date) - 1
					while ( date < parseInt(endObj.date) ) {
						date++
						dates.push({
							label: Util.zeroize(date) + '日',
							value: Util.zeroize(date)
						})
					}
				}
				this.default({
					title: title || '选择日期',
					value: value,
					itemList: dates,
					cascade: arr.length,
					confirm: confirm,
					cancel: cancel,
					success: success,
					fail: fail,
					options: options
				})
			},
			pickerConfirm () {
				this.success ? this.success({
					confirm: true,
					data: {
						value: this.values
					}
				}) : null
				this.visible = false
			},
			pickerCancel () {
				this.success ? this.success({
					confirm: false
				}) : null
				this.visible = false
			},
			onChange (e) {
				this.values = e
			},
			hide () {
				this.visible = false
				this.fail ? this.fail() : null
			}
		}
	}
</script>

<style>
	.yb-picker {
		padding: 30rpx;
	}
</style>